package com.zen.lon.open.remote.support;

import com.zen.lon.open.proxy.ProxyAgentApplicationCleaner;
import com.zen.lon.open.remote.httprequest.interceptor.RestTemplateTraceInterceptor;
import com.zen.lon.open.remote.annotation.EnableRemoteClient;
import com.zen.lon.open.remote.sign.DefaultInvokeSign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * 自动注入RemoteClient
 *
 * @author ZonLen since on 2021/6/30 上午9:16
 */

@EnableRemoteClient
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnProperty(name = "remote.client.enable", havingValue = "true", matchIfMissing = true)
public class RemoteClientAutoConfiguration {


  @Bean
  public ProxyAgentApplicationCleaner ProxyAgentObjectApplicationCleaner() {
    return new ProxyAgentApplicationCleaner();
  }

//  @Bean
//  public ProxyAgentMetaDataReader ProxyAgentObjectMetaDataReader() {
//    return new RemoteClientMetaDataReader();
//  }

  @Bean("remoteClientRestTemplate")
  public RestTemplate remoteClientRestTemplate() {
    final SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    final RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getInterceptors().add(new RestTemplateTraceInterceptor());
    factory.setConnectTimeout(3000);
    factory.setReadTimeout(2000);
    return restTemplate;
  }


  /**
   * 简化实现方式，在当前配置项中定义bean，可优化到外部
   */
  @Bean
  public DefaultInvokeSign defaultSignHandler() {
    return new DefaultInvokeSign();
  }

}
